﻿using System;
using System.Collections.Generic;
using System.Linq;
using Dexter.Search.Lucene.DocumentDefinition;
using Lucene.Net.Documents;

namespace Dexter.Search.Lucene.Wrapper {
	public class SearchResult<T> {
		private readonly IResultDefinition<T> definition;
		private readonly IEnumerable<Document> documents;
		private IEnumerable<T> entities;

		public SearchResult ( IEnumerable<Document> documents, IResultDefinition<T> definition ) {
			this.documents = documents;
			this.definition = definition;
		}

		public IEnumerable<Document> Documents {
			get {
				return this.documents;
			}
		}

		public IEnumerable<T> Results {
			get {
				if (( this.entities == null ) && ( this.definition != null )) {
					Func<Document, T> selector = doc => definition.Convert ( doc );
					this.entities = this.documents.Select ( selector ).ToList ( );
				}
				return this.entities;
			}
		}
	}
}
